<?php

class AbstractRule
{
    protected $pdo;
    protected $preProcessing;
    
    public function __construct(\PDO $pdo = null)
    {
        if ($pdo == null) {
            $dsn      = 'mysql:dbname=qas_quran; host=127.0.0.1; port=3306';
            $username = 'root';
            $password = '1234';
            $options  = array(
                PDO::MYSQL_ATTR_INIT_COMMAND => 'SET NAMES utf8',
            ); 

            try {
                $pdo  = new \PDO($dsn, $username, $password, $options);
                $pdo->setAttribute(\PDO::ATTR_ERRMODE, \PDO::ERRMODE_EXCEPTION);
                $this->pdo = $pdo;
            } catch (\PDOException $e) {
                ?>
                <div class="row">
                    <div class="col-lg-12">
                        <h1 class="page-header">ERROR</h1>
                    </div>
                </div>
                <?php
                echo '<div class="alert alert-danger">Connection failed : ' . $e->getMessage().'</div>';
                exit;
            }
        } else {
            $this->pdo = $pdo;
        }

        $this->preProcessing = new PreProcessing($this->pdo);
    }
    
    protected function wordMacthScooring($kata)
    {
        $result = array();
        
        $sql     = "SELECT * FROM daftar_kata_quran WHERE LOWER(kata) = '".strtolower($kata)."'";
        foreach ($this->pdo->query($sql) as $row) {
            $score = 3;
            if (strtolower($row['jenis_kata']) == 'verba') {
                $score = 6;
            }
            $result[$row['sura'].'-'.$row['aya']] = array(
                'sura'  => $row['sura'],
                'aya'   => $row['aya'],
                'kata'  => $row['kata'],
                'score' => $score,
            );
        }
        
        return $result;
    }

    protected function getAyatIndonesia(array $listSura, array $listAya, array $listSuraAya)
    {
        $result = array();

        $surat = implode("', '", $listSura);
        $ayat  = implode("', '", $listAya);
        
        $sql     = "SELECT * FROM quran_text_indonesia WHERE sura IN ('".$surat."') AND aya IN ('".$ayat."')";
        foreach ($this->pdo->query($sql) as $row) {
            if (in_array($row['sura'], $listSura)) {
                if (in_array($row['aya'], $listSuraAya[$row['sura']])) {
                    $result[$row['sura']][$row['aya']] = $row['text'];
                }
            }
        }
        
        return $result;
    }

    protected function getAyatArabic(array $listSura, array $listAya, array $listSuraAya)
    {
        $result = array();

        $surat = implode("', '", $listSura);
        $ayat  = implode("', '", $listAya);
        
        $sql     = "SELECT * FROM quran_text_uthmani WHERE sura IN ('".$surat."') AND aya IN ('".$ayat."')";
        foreach ($this->pdo->query($sql) as $row) {
            if (in_array($row['sura'], $listSura)) {
                if (in_array($row['aya'], $listSuraAya[$row['sura']])) {
                    $result[$row['sura']][$row['aya']] = $row['text'];
                }
            }
        }
        
        return $result;
    }

    protected function getNamaSurat(array $listSura)
    {
        $result = array();

        $surat = implode("', '", $listSura);
        
        $sql     = "SELECT * FROM sura WHERE `index` IN ('".$surat."')";
        foreach ($this->pdo->query($sql) as $row) {
            if (in_array($row['index'], $listSura)) {
                $result['indonesia'][$row['index']] = $row['name_indonesia'];
                $result['arabic'][$row['index']] = $row['name_arabic'];
            }
        }
        
        return $result;
    }
    
    protected function contains(array $listKata, $score, array $listSura, array $listSuraAya)
    {
        $result = array();
        $kataKata = implode("', '", $listKata);
        
        $sql     = "SELECT * FROM daftar_kata_quran WHERE LOWER(kata) IN ('".$kataKata."')";
        foreach ($this->pdo->query($sql) as $row) {
            if (in_array($row['sura'], $listSura)) {
                if (in_array($row['aya'], $listSuraAya[$row['sura']])) {
                    $result[$row['kata']][$row['sura'].'-'.$row['aya']] = array(
                        'sura'  => $row['sura'],
                        'aya'   => $row['aya'],
                        'kata'  => $row['kata'],
                        'score' => $score,
                    );
                }
            }
        }
        
        return $result;
    }

    protected function containsWords(array $question, array $words)
    {
        $isContainWord = false;
        $jumlah = 0;

        foreach ($question as $kata) {
            if (in_array($kata, $words)) {
                $isContainWord = true;
                $jumlah++;
            }    
        }

        return $isContainWord;
    }

    protected function setTotalScoreRule(array $result)
    {
        foreach ($result as $sura => $dataSura) {
            foreach ($dataSura as $aya => $dataAya) {
                $totalAll = 0;
                foreach ($dataAya as $jenis => $dataJenis) {
                    $total = 0;
                    foreach ($dataJenis as $key => $dataKata) {
                        $total += $dataKata['score'];
                    }
                    $result[$sura][$aya]['total '.$jenis] = $total;
                    $totalAll += $total;
                }
                $result[$sura][$aya]['total'] = $totalAll;
            }
        }

        return $result;
    }

    protected function setAyatIndonesia(array $ayatIndonesia, array $result)
    {
        foreach ($ayatIndonesia as $sura => $dataSura) {
            foreach ($dataSura as $aya => $dataAya) {
                $result[$sura][$aya]['indonesia'] = $dataAya;
            }
        }

        return $result;
    }

    protected function setAyatArabic(array $ayatArabic, array $result)
    {
        foreach ($ayatArabic as $sura => $dataSura) {
            foreach ($dataSura as $aya => $dataAya) {
                $result[$sura][$aya]['arabic'] = $dataAya;
            }
        }

        return $result;
    }

    protected function sortingResult(array $result)
    {
        $dataTotal = array();
        $dataAyat  = array();
        foreach ($result as $sura => $dataSura) {
            foreach ($dataSura as $aya => $dataAya) {
                foreach ($dataAya as $jenis => $dataJenis) {
                    if ($jenis == 'total') {
                        $dataTotal[] = $dataJenis;
                    }
                }
                $dataAyat[] = array(
                    'sura' => $sura,
                    'aya'  => $aya,
                    'data' => $dataAya,
                );
            }
        }
        array_multisort($dataTotal, SORT_NUMERIC, SORT_DESC, $dataAyat);

        return $dataAyat;
    }

    protected function getKamusOrang()
    {
        $sql  = "SELECT * FROM `kamus_orang`";
        $data = array();
        foreach ($this->pdo->query($sql) as $row) {
            $data[$row['kata']] = $row['kata'];
        }
        return $data;
    }

    protected function getKamusTempat()
    {
        $sql  = "SELECT * FROM `kamus_tempat`";
        $data = array();
        foreach ($this->pdo->query($sql) as $row) {
            $data[$row['kata']] = $row['kata'];
        }
        return $data;
    }

    protected function getPreposisiTempat()
    {
        return array(
            'di', 'pada', 'dalam', 'atas', 'antara', 'bawah', 'belakang'
        );
    }

    protected function getKamusWaktu()
    {
        $sql  = "SELECT * FROM `kamus_waktu`";
        $data = array();
        foreach ($this->pdo->query($sql) as $row) {
            $data[$row['kata']] = $row['kata'];
        }
        return $data;
    }
    
    /* Fungsi untuk mendapatkan sinonim
     * @param string $kata
     * @return array
     */
    protected function getSinonimKata($kata, $tipe)
    {
        $sinonims = array();

        if (!empty($tipe)) {
            $sql     = "SELECT * FROM sinonim WHERE LOWER(kata) = '".$kata."' AND jenis_kata = '".$tipe."' AND jenis_padanan_kata = '".$tipe."'";
            foreach ($this->pdo->query($sql) as $row) {
                $explodeKata = explode(" ", $row['padanan_kata']);
                if (count($explodeKata) < 2) {
                    $sinonims[$row['padanan_kata']] = $row['padanan_kata'];
                }
            }
        }
        
        return $sinonims;
    }

    protected function getTipeKata($kata)
    {
        $tipe = null;
        $sql  = "SELECT * FROM kata_dasar WHERE LOWER(kata) = '".$kata."'";
        foreach ($this->pdo->query($sql) as $row) {
            $tipe = $row['tipe'];
        }
        return $tipe;
    }
}

